import request from "./request.js";

/**
 * 获取图形验证码
 * @returns {Promise<string>}
 */
export const getCaptcha = () => {
    return request.get('/captcha?time=' + new Date().getTime() * Math.random(), {
        responseType: "arraybuffer"
    }).then(response => {
        return 'data:image/png;base64,' +
            btoa(new Uint8Array(response.data)
                .reduce((data, byte) => data + String.fromCharCode(byte), ''));
    })
}

/**
 * 文件上传
 * @param file
 * @returns {Promise<axios.AxiosResponse<any>>}
 */
export const uploadFile = (file) => {
    return request.post('/tencent/cos/upload', {
        file
    }, {
        headers: {
            'Content-Type': 'multipart/form-data'
        }
    })
}

/**
 * 获取邮箱验证码
 * @param data
 * @returns {Promise<AxiosResponse<any>>}
 */
export const getEmailCaptcha = (data) => request.post('/email/send', data)

// 登录
export const login = data => request.post('/login', data)

// 查询系统设置信息
export const querySystemSettings = () => request.get('/sysSettings')

// 通过id查询用户
export const queryUserByUserId = userId => request.get(`/user/${userId}`)

// 查询从库的全部表(自定义的)
export const queryTables = () => request.post('/codeGenerator/tables')

// 代码生成
export const doCodeGenerate = data => request.post('/codeGenerator/generate', data)


// 连接测试
export const doConnectionTest = data => request.post('/codeGenerator/connectionTest', data)
